# -*- coding: utf-8 -*-
import os
import maya.cmds as mc
from liberHook.BaseHook import BaseHook
from liberLibs.dccLibs import Maya


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        BaseHook.__init__(self, library, directory, ext, start, end)

    def execute(self):
        f = self.file
        if not os.path.isfile(f):
            self.append_error("%s is not an exist file." % f)
            return
        sels = Maya.selected()
        if not sels:
            self.append_error("Nothing selected")
            return
        old_sg_nodes = mc.ls(type="shadingEngine")
        Maya.maya_import(f)
        new_sg_nodes = mc.ls(type="shadingEngine")
        sg_node = list(set(new_sg_nodes)-set(old_sg_nodes))
        if not sg_node:
            self.append_error("No shadingEngine node import from %s" % f)
            return
        mc.sets(sels, fe=sg_node[0])
        Maya.delete_unused_nodes()
